<?php

class M_item extends CI_Model
{
     protected $_table = "item";
    function get_all_item()
    {
        $result= $this->db->get('item');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
    }
     function update_sale_chung($from,$to,$saleoffchung)
    {
        $kq1= $this->update_gia_chung($saleoffchung);
        $kq1=$this->update_start($from);
        $kq1=$this->update_end($to);
        return 1;
    }
        function update_gia_chung($sale)
        {
            $data['SAMPLE_VALUE'] = $sale;
            return $this->db->update('sample_data',$data,array('SAMPLE_ID'=>1));
        }
        function update_start($from)
        {
             $data['SAMPLE_VALUE'] = $from;
            return $this->db->update('sample_data',$data,array('SAMPLE_ID'=>2));
        }
        function update_end($to)
        {
             $data['SAMPLE_VALUE'] = $to;
            return $this->db->update('sample_data',$data,array('SAMPLE_ID'=>3));
        }
    function update_sale_rieng($id,$from,$to,$saleoffrieng)
    {
        $data['ITEM_ID'] = $id;
        $data['START_DATE'] = $from ;
        $data['END_DATE'] = $to ;
        $data['SOVALUE'] = $saleoffrieng;
        if($saleoffrieng == 0)
        {
           $kq= $this->update_saleoff($id,0);
        } else $kq = $this->update_saleoff($id,1);
        return $this->db->insert('sale_off',$data);
    }
    function get_new_saleoff_chung()
    {
        $result = $this->db->get('sample_data');
        	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;
    }
    function get_new_saleoff_by_itemid($id)
    {
        $this->db->where('ITEM_ID', $id);
        $this->db->limit(1);
        $this->db->order_by('SALEOFF_ID desc');
    	$result = $this->db->get('sale_off');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;
    }
    function update_saleoff($id,$sale)
    {
         $data['IS_SALE'] = $sale;
        return $this->db->update('item',$data,array('ITEM_ID'=>$id));
    }
    function update_item($id,$ITEM_NAME,$HERO_NAME,$ITEM_RARITY,$ITEM_TYPE,$ITEM_IMAGE,$DESCRIPTION,$IS_HOT_ITEM,$IS_SIDEBAR_ITEM,$IS_SLIDE,$IS_SALE)
    {
        $data['ITEM_NAME'] = $ITEM_NAME;
        $data['HERO_NAME'] = $HERO_NAME;
        $data['ITEM_RARITY'] = $ITEM_RARITY;
        $data['ITEM_TYPE'] = $ITEM_TYPE;
        $data['ITEM_IMAGE'] = $ITEM_IMAGE;
        $data['DESCRIPTION'] = $DESCRIPTION;
        $data['IS_HOT_ITEM'] = $IS_HOT_ITEM;
        $data['IS_SIDEBAR_ITEM'] = $IS_SIDEBAR_ITEM;
        $data['IS_SLIDE'] = $IS_SLIDE;
        $data['IS_SALE'] = $IS_SALE ;
        return $this->db->update('item',$data,array('ITEM_ID'=>$id));
    }
    function add_item($ITEM_NAME,$HERO_NAME,$ITEM_RARITY,$ITEM_TYPE,$ITEM_IMAGE)
    {
        $data['ITEM_NAME'] = $ITEM_NAME ;
        $data['HERO_NAME'] = $HERO_NAME ;
        $data['ITEM_RARITY'] = $ITEM_RARITY ;
        $data['ITEM_TYPE'] = $ITEM_TYPE ;
        $data['ITEM_IMAGE'] = $ITEM_IMAGE ;
        $data['IS_EDIT'] = 1;
        return $this->db->insert('item',$data);
    }
    function update_price($id,$ngaycapnhat,$giamoi)
    {
            $data['ITEM_ID']=$id;
            $data['MODIFIED_DATE']=$ngaycapnhat;
            $data['PRICE']=$giamoi;
            return $this->db->insert('price_list',$data);
    }
    function get_all_rarity()
    {
        $this->db->select('ITEM_RARITY');
        $this->db->distinct();
        $result= $this->db->get('item');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
    }
      function get_all_hero()
    {
        $this->db->select('HERO_NAME');
        $this->db->distinct();
        $this->db->order_by('HERO_NAME','asc');
        $result= $this->db->get('item');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
    }
    function get_all_type()
    {
        $this->db->select('ITEM_TYPE');
        $this->db->distinct();
        $result= $this->db->get('item');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;
    }
       public function get_item_by_id($id)
    {
         $query=$this->db->query("SELECT item.*, temp.PRICE
                                        FROM item
                                        LEFT JOIN (
                                            select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                            	from price_list a
                                            join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                            	on a.PRICE_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            ) temp
                                        ON item.ITEM_ID = temp.ITEM_ID
                                    
                                        where item.ITEM_ID = $id
                                        ");
                                        
            if($query->num_rows()>0)
                return $query->row();
            else return false;
    }
    public function get_item_by_name($name,$limit,$start)
    {
         $query=$this->db->query("SELECT item.*, temp.PRICE
                                        FROM item
                                        LEFT JOIN (
                                            select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                            	from price_list a
                                            join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                            	on a.PRICE_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            ) temp
                                        ON item.ITEM_ID = temp.ITEM_ID
                                        where ITEM_NAME like '%$name%'
                                            order by SET_ITEMS_NAME
                                         LIMIT ? OFFSET ?
                                        ",array($limit,$start));
                                        
            if($query->num_rows()>0)
                return $query->result();
            else return false;
    }
       public function get_item_by_name_count($name)
    {
         $query=$this->db->query("SELECT item.*, temp.PRICE
                                        FROM item
                                        LEFT JOIN (
                                            select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                            	from price_list a
                                            join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                            	on a.PRICE_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            	
                                            ) temp
                                        ON item.ITEM_ID = temp.ITEM_ID
                                        where ITEM_NAME like '%$name%'
                                            order by SET_ITEMS_NAME
                                        ");
                return $query->num_rows();
    }
    public function lay_item_warning()
    {
        $query=$this->db->query('SELECT item.*, temp.PRICE
                                        FROM item
                                        LEFT JOIN (
                                            select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                            	from price_list a
                                            join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                            	on a.PRICE_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            	
                                            ) temp
                                        ON item.ITEM_ID = temp.ITEM_ID
                                        where PRICE > PRICE_MARKET
                                            order by SET_ITEMS_NAME
                                        ');
            if($query->num_rows()>0)
                return $query->result();
            else return false;
    }
    public function get_all_item_2()
    {
            $query=$this->db->query('SELECT item.*, temp.PRICE
                                        FROM item
                                        LEFT JOIN (
                                            select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                            	from price_list a
                                            join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                            	on a.PRICE_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            	
                                            ) temp
                                        ON item.ITEM_ID = temp.ITEM_ID
                                            order by SET_ITEMS_NAME
                                        ');
            if($query->num_rows()>0)
                return $query->result();
            else return false;
    }
     
    /* public function listall($offset,$start){
         $this->db->limit($offset,$start);
        $this->db->order_by("id","desc");
         return $this->db->get($this->_table)->result_array();
     }*/
     public function count_data($he,$ra,$ty)
     {
         $query=$this->db->query("SELECT item.*, temp.PRICE
                                        FROM item
                                        LEFT JOIN (
                                            select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                            	from price_list a
                                            join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                            	on a.PRICE_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            	
                                            ) temp
                                        ON item.ITEM_ID = temp.ITEM_ID
                                        where HERO_NAME like '%$he%' and ITEM_RARITY like '%$ra%' and ITEM_TYPE like '%$ty%'
                                            order by SET_ITEMS_NAME
                                        ");
         
                                        return $query->num_rows();
     }
     public function lay_danh_sach_san_pham_phan_trang($he,$ra,$ty,$limit,$start)
        {
           /* $this->db->limit($limit);
            $this->db->offset($start);
            $result= $this->db->get('item');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;*/
                $query=$this->db->query("SELECT item.*, temp.PRICE
                                        FROM item
                                        LEFT JOIN (
                                            select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                            	from price_list a
                                            join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                            	on a.PRICE_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            	
                                            ) temp
                                        ON item.ITEM_ID = temp.ITEM_ID
                                        where HERO_NAME like '%$he%' and ITEM_RARITY like '%$ra%' and ITEM_TYPE like '%$ty%'
                                            order by SET_ITEMS_NAME
                                         LIMIT ? OFFSET ?
                                        ",array($limit,$start));
                                        
            if($query->num_rows()>0)
                return $query->result();
            else return false;
        }
         public function lay_danh_sach_san_pham_phan_trang2($limit,$start)
        {
           /* $this->db->limit($limit);
            $this->db->offset($start);
            $result= $this->db->get('item');
            if($result->num_rows()>0)
                return $result->result();
            else
                return false;*/
                $query=$this->db->query("SELECT item.*, temp.PRICE
                                        FROM item
                                        LEFT JOIN (
                                            select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                            	from price_list a
                                            join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                            	on a.PRICE_ID = b.id
                                            	and b.Ngay_max = a.MODIFIED_DATE
                                            	
                                            ) temp
                                        ON item.ITEM_ID = temp.ITEM_ID
                                            order by SET_ITEMS_NAME
                                         LIMIT ? OFFSET ?
                                        ",array($limit,$start));
                                        
            if($query->num_rows()>0)
                return $query->result();
            else return false;
        }

        public function lay_danh_sach_san_pham_phan_trang_theo_loai($type,$limit,$start)
        {
                $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                        WHERE ITEM_TYPE = ?
                                            order by SET_ITEMS_NAME
                                         LIMIT ? OFFSET ?
                                        ",array($type,$limit,$start));
                                        
            if($query->num_rows()>0)
                return $query->result();
            else return false;
        }
        public function lay_danh_sach_san_pham_phan_trang_theo_hero($hero,$limit,$start)
        {
                $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                        WHERE HERO_NAME = ? and ITEM_TYPE = 'bundle'
                                            order by SET_ITEMS_NAME
                                         LIMIT ? OFFSET ?
                                        ",array($hero,$limit,$start));
                                        
            if($query->num_rows()>0)
                return $query->result();
            else return false;
        }
        public function count_data_by_hero($hero)
        {
               $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            where HERO_NAME = ? and ITEM_TYPE = 'bundle'
                                                order by SET_ITEMS_NAME
                                            ", $hero);
            return $query->num_rows();
        }
        public function count_data_by_type($type)
        {
            $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            where ITEM_TYPE = ?
                                                order by SET_ITEMS_NAME
                                            ", $type);
            return $query->num_rows();
        }

     public function count_all(){
         return $this->db->count_all($this->_table);
     }
    
	function get_slide_item()
	{

        $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            where IS_SLIDE = 1
                                            ");
            if($query->num_rows()>0)
                return $query->result();
            else return false;

		/*$this->db->where('IS_SLIDE',1);
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;*/
	}


    function get_hot_item()
    {
        $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            where IS_SLIDE = 1
                                            ");
            if($query->num_rows()>0)
                return $query->result();
            else return false;

    	/*$this->db->where('IS_HOT_ITEM',1);
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;*/
    }

    function get_sidebar_item()
    {
        $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            where IS_SIDEBAR_ITEM = 1
                                                limit 1
                                            ");
            if($query->num_rows()>0)
                return $query->result();
            else return false;

    	/*$this->db->where('IS_SIDEBAR_ITEM',1);
    	$this->db->limit(1);
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;*/
    }

    function get_treasure_sidebar_item()
    {
    	$this->db->where('IS_SIDEBAR_ITEM',1);
    	$this->db->where('ITEM_TYPE','treasure_chest');
    	$this->db->limit(1);
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;
    }

    function get_tournament_sidebar_item()
    {
        $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            where ITEM_TYPE = 'league' and IS_SIDEBAR_ITEM = 1
                                                limit 3
                                            ");
            if($query->num_rows()>0)
                return $query->result();
            else return false;

    	/*$this->db->where('IS_SIDEBAR_ITEM',1);
    	$this->db->where('ITEM_TYPE','league');
    	$this->db->limit(3);
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;*/
    }

    function get_HUD_Skin_sidebar_item()
    {
        $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            where ITEM_TYPE = 'hud_skin' and IS_SIDEBAR_ITEM = 1
                                                limit 3
                                            ");
            if($query->num_rows()>0)
                return $query->result();
            else return false;
    	/*$this->db->where('IS_SIDEBAR_ITEM',1);
    	$this->db->where('ITEM_TYPE','hud_skin');
    	$this->db->limit(3);
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;*/
    }

    function get_new_release_item()
    {
        $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            where  DATEDIFF(CURDATE(),ADD_DATE)  <= 7
                                            ");
            if($query->num_rows()>0)
                return $query->result();
            else return false;

    	/*$this->db->where('ADD_DATE <=','DATE_ADD(NOW(),INTERVAL 7 DAYS )');
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;*/
    }

    function get_top_seller_item()
    {
        $query=$this->db->query("SELECT item.*, temp.PRICE
                                            FROM item
                                            LEFT JOIN (
                                                select a.ITEM_ID, a.MODIFIED_DATE, a.PRICE
                                                    from price_list a
                                                join (select max(PRICE_ID)as id, ITEM_ID, max(modified_date) as Ngay_max from price_list group by ITEM_ID) b
                                                    on a.PRICE_ID = b.id
                                                    and b.Ngay_max = a.MODIFIED_DATE
                                                    
                                                ) temp
                                            ON item.ITEM_ID = temp.ITEM_ID
                                            inner join (
                                           select item_id, count(invoice_id) as temp from invoice_detail group by item_id order by temp desc limit 10) temp1
                                           on item.ITEM_ID = temp1.ITEM_ID
                                            ");
            if($query->num_rows()>0)
                return $query->result();
            else return false;

    }

    function get_list_item_by_type($type)
    {
    	$this->db->where('ITEM_TYPE', $type);
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;
    }
    
	function get_list_item_by_rarity($rarity)
    {
    	$this->db->where('ITEM_RARITY', $rarity);
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;
    }

    function get_Info_item_by_id($id)
    {
    	$this->db->where('ITEM_ID', $id);
    	$result = $this->db->get('item');
    	if($result->num_rows()>0)
    		return $result->result();
    	else
    		return false;
    }

    function get_preview_image_by_item_id($id)
    {
        $this->db->where('ITEM_ID', $id);
        $result = $this->db->get('item_preview');
        if($result->num_rows()>0)
            return $result->result();
        else
            return false;
    }

    function get_related_item($set_name, $id)
    {
        $query=$this->db->query(" SELECT * FROM item WHERE ITEM_ID != ? AND SET_ITEMS_NAME = ? ",array($id, $set_name));
        if($query->num_rows()>0)
            return $query->result();
        else
            return false;
    }
    
}


?>